package com.leon.alishop.web.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.leon.alishop.core.bo.GoodsBo;
import com.leon.alishop.core.bof.GoodsBof;
import com.leon.alishop.core.enumeration.GoodsStatusEnum;
import com.leon.alishop.core.page.Page;
import com.leon.alishop.core.vo.GoodsParams;
import com.leon.alishop.dao.GoodsInfoDao;
import com.leon.alishop.model.GoodsInfo;
import com.leon.alishop.web.util.AlishopImageUtils;

@Controller
@RequestMapping(value = "/goods")
public class GoodsController  extends BaseController{

	private final static String GOODS_LIST_PAGE = "/goods/list";
	private final static String GOODS_LIST_REDIRECT= "redirect:/goods/list.htm";
	
	private final static String PATH = "e:/ALISHOP-IMAGE/";
	
	@RequestMapping(value = "/list")
	public String viewGoodsList(GoodsParams vo,BindingResult bindingResult, HttpServletRequest req,HttpServletResponse res,Model model){
		if(null == vo)
			vo = new GoodsParams();
		Page page = new Page(100);
		if(vo.getP()==0){
			page.setPageNo(1);
		}else{
			page.setPageNo(vo.getP());
		}
		List<GoodsBo> goodsBoList = goodsBof.showAllGoodsBoList(vo, page);
		model.addAttribute("page", page);
		model.addAttribute("goodsBoList", goodsBoList);
		model.addAllAttributes(new BeanMap(vo));
		return GOODS_LIST_PAGE;
	}
	
	@RequestMapping(value = "/change-status")
	public String changeStatus(@RequestParam Long id,@RequestParam String status,Model model){
		if(null == id)
			return GOODS_LIST_REDIRECT;
		GoodsInfo gi = new GoodsInfo();
		gi.setId(id);
		gi.setStatus(GoodsStatusEnum.valueOf(status).ordinal());
		goodsInfoDao.updateByIdSelective(gi);
		return GOODS_LIST_REDIRECT + "#J_item_"+id;
	}
	
	@RequestMapping(value = "/download-image")
	public String downImageToLocal(@RequestParam Long id,Model model){
		if(null == id)
			return GOODS_LIST_REDIRECT;
		GoodsInfo goodsInfo = goodsInfoDao.selectById(id);
		if(null == goodsInfo 
				|| StringUtils.isBlank(goodsInfo.getProviderGoodsImgInfo()))
			return GOODS_LIST_REDIRECT;
		Map imgMap = null;
		imgMap = JSONObject.fromObject(goodsInfo.getProviderGoodsImgInfo());
		if(imgMap == null)
			return GOODS_LIST_REDIRECT;
		List<String> imgList = (List<String>) imgMap.get("min");
//		AlishopImageUtils.deleteAllFile(PATH);
		String path = PATH + "/"+id+"/";
		if(null != imgList){
			String[] imgArr = new String[imgList.size()];
			for (int i = 0,len = imgList.size(); i < len; i++) {
				imgArr[i] = AlishopImageUtils.splitImgMinToBigUrl(imgList.get(i));
			}
			try {
				AlishopImageUtils.downloadImgToLocal(path,"MIN-"+id.toString()+"-",imgArr);
			} catch (HttpException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		imgList = (List<String>) imgMap.get("big");
		if(null != imgList){
			try {
				AlishopImageUtils.downloadImgToLocal(path,"BIG-"+id.toString()+"-",imgList.toArray(new String[imgList.size()]));
			} catch (HttpException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return GOODS_LIST_REDIRECT;
	}
	
	@Autowired
	private GoodsBof goodsBof;
	@Autowired
	private GoodsInfoDao goodsInfoDao;
	
}
